package com.wish.consumer.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.wish.consumer.entity.Consumer;
import com.wish.consumer.service.IConsumerService;
import com.wish.dto.contract.ConsumerDto;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author lyk
 * @since 2022-08-19
 */
@RestController
@RequestMapping("/consumer")
public class ConsumerController {

    @Autowired
    private IConsumerService consumerService;

    @RequestMapping("/list")
    public List<ConsumerDto> list(Integer type) {
        QueryWrapper<Consumer> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("type",type);
        List<Consumer> list = consumerService.list(queryWrapper);
        List<ConsumerDto> consumerDtoList=new ArrayList<>();
        for (Consumer consumer : list) {
            ConsumerDto dto = new ConsumerDto();
            BeanUtils.copyProperties(consumer,dto);
            consumerDtoList.add(dto);
        }
        return consumerDtoList;
    }
    @RequestMapping("/ByNameGetConsumer")
    public List<ConsumerDto> ByNameGetConsumer(String name,Integer type) {
        QueryWrapper<Consumer> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("type",type);
        queryWrapper.like("name", name);

        List<Consumer> list = consumerService.list(queryWrapper);
        List<ConsumerDto> consumerDtoList=new ArrayList<>();
        for (Consumer consumer : list) {
            ConsumerDto dto = new ConsumerDto();
            BeanUtils.copyProperties(consumer,dto);
            consumerDtoList.add(dto);
        }
        return consumerDtoList;
    }


}
